package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.databinding.RecipeLitemBinding;
import com.example.myapplication.entity.ResultsBean;

import java.util.List;

public class MyRecyclerviewAdapter extends RecyclerView.Adapter<MyRecyclerviewAdapter.ViewHolder> {
    private List<ResultsBean> reciptesList;
    private Context context;
    public MyRecyclerviewAdapter(List<ResultsBean> reciptesList,Context context){
        this.reciptesList = reciptesList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecipeLitemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recipe_litem, null, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ResultsBean resultsBean = reciptesList.get(position);
        holder.recipeLitemBinding.tvId.setText(resultsBean.getTitle());
        Glide.with(context)
                .load(resultsBean.getCover())
                .into(holder.recipeLitemBinding.ivPic);
    }

    @Override
    public int getItemCount() {
        return reciptesList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RecipeLitemBinding recipeLitemBinding;
        public ViewHolder(@NonNull RecipeLitemBinding recipeLitemBinding) {
            super(recipeLitemBinding.getRoot());
            this.recipeLitemBinding=recipeLitemBinding;
        }
    }
}
